<?php

namespace App\Http\Controllers\Backend\Product;

 
use App\ConstDir\ErrorConst;
use App\Http\Controllers\Backend\BaseController;
use App\Repository\Repositories\Interfaces\ProductCategoriesRepository;
use Illuminate\Http\Request;
use App\Repository\Repositories\Interfaces\AttributesRepository;
 
 
class CategoryController extends BaseController
{
    protected $request;
    protected $category_repo;

    public function __construct(ProductCategoriesRepository $category_repo,Request $request)
    {
        $this->category_repo = $category_repo;
        $this->request = $request;
    }
    
    public function index(Request $request)
    {
        
        $type = $request->input("type","");
        
        if($type == "all"){
            $cates = $this->category_repo->orderBy("root_id","asc")->orderBy("pid","asc")->orderBy("sort","asc")->orderBy("id","asc")->all();
            return $this->success(ErrorConst::SUCCESS_CODE, $cates,false);
        }else{
            $cates = $this->category_repo->paginate($this->perPage);
            return $this->success(ErrorConst::SUCCESS_CODE, $cates,true);
        }
        
    }
    
    public function show($id)
    {
        $cate = $this->category_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE, $cate);
    }
    
    public function create(Request $request, AttributesRepository $attr_repo)
    {
        
        $this->validate($request,[
            'name'=> 'required|string',
            'initial' => 'required|string|size:1',
            'cat_sn'=>'string',
            'en_name'=>'string',
            'pid'=>'required|string',
            'sort'=>'integer',
        ]);
        
        $data = $request->all();
        
        if(isset($data['attrs'])){
            $attrs= $attr_repo->getAttrs($data['attrs']);
            if(isset($attrs['data']) && $attrs['data'] ){
                $attrs = $attrs['data'];
                $attrs_arr = [];
                foreach($attrs as $attr){
                    $av = [];
                    foreach($attr['attr_values'] as $val){
                        $av[$val['id']]=$val['value'];
                    }
                    $attrs_arr[$attr['id']]=['name'=>$attr['name'],'is_sku'=>$attr['is_sku'],'attribute_id'=>$attr['id'],'values'=>$av];
                }
                
                $data['attrs']  = json_encode($attrs_arr);
            }
        }
        
        
        $cate = $this->category_repo->create($data);
        if($cate){
            return $this->success(ErrorConst::SUCCESS_CODE, $cate['data']);
        }else{
            return $this->error(ErrorConst::FAILED_CODE);
        }
        
    }
    
    public function update(Request $request, AttributesRepository $attr_repo, $id)
    {
        $this->validate($request,[
            'name'=> 'required|string',
            'initial' => 'required|string|size:1',
        ]);
        $data = $request->all();
        
        if(isset($data['attrs'])){
            $attrs= $attr_repo->getAttrs($data['attrs']);
            if(isset($attrs['data']) && $attrs['data'] ){
                $attrs = $attrs['data'];
                $attrs_arr = [];
                foreach($attrs as $attr){
                    $av = [];
                    foreach($attr['attr_values'] as $val){
                        $av[$val['id']]=$val['value'];
                    }
                    $attrs_arr[$attr['id']]=['name'=>$attr['name'],'is_sku'=>$attr['is_sku'],'attribute_id'=>$attr['id'],'values'=>$av];
                }
                
                $data['attrs']  = json_encode($attrs_arr);
            }
        }
        
        $ret = $this->category_repo->update($data, $id);
        if($ret){
            return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
        }else{
            return $this->error(ErrorConst::FAILED_CODE);
        }
    }
    
    public function delete($id)
    {
        $ret = $this->category_repo->delete($id);
        
        
        if ($ret) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

    /**
     * 设置类目是否为推荐
     * */
   public function recommend($id){
       $status = $this->request->get('rec');
       $status = $status==1?0:1;

       $ret = $this->category_repo->setRecommend($id,$status);

       if ($ret) {
           return $this->success('修改成功');
       } else {
           return $this->error('修改失败');
       }
   }
   
   public function showstatus($id){
       $status = $this->request->get('status');
       $status = $status==1?0:1;
       
       $ret = $this->category_repo->setShowstatus($id,$status);
       
       if ($ret) {
           return $this->success('修改成功');
       } else {
           return $this->error('修改失败');
       }
   }


    /**
    * 设置类目是否为推荐
    * */
    public function shopindex_recommend($id){
     $status = $this->request->get('rec');
     $status = $status==1?0:1;

     $ret = $this->category_repo->setShopIndexRecommend($id,$status);

     if ($ret) {
         return $this->success('修改成功');
     } else {
         return $this->error('修改失败');
     }
    }
}
